// pages/login/login.js
import API from '../../utils/api'
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    login:true,
    logins:'',
    password:'123456',
    username:'15730131874',
    usertell:'',
    isAgree: true
  },
  radioChange:function(e){
    this.setData({
      //取反
      isAgree: !this.data.isAgree,
    });
  },
  password:function(e){
    var val = e.detail.value; 
    this.setData({
      password: val
    })
    //console.log(this.data.password)
  },
  username: function (e) {
    var val = e.detail.value; 
    this.setData({
      username: val
    })
    //console.log(this.data.username)
  },
  
  loginTaps:function(e){
    var that = this;
    var name = e.currentTarget.dataset.name;
    var password = e.currentTarget.dataset.password;
    var userRegex =/^(([0-9A-Za-z\-_\.]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?))|(^1[3|4|5|6|7|8|9]\d{9})$/
    console.log(userRegex.test(name))
    if (!userRegex.test(name) ) {
      wx.showModal({
        title: '温馨提示',
        content: '请输入正确的用户名'
      })
    } else if (password == "") {
      wx.showModal({
        title: '温馨提示',
        content: '请输入正确的密码'
      })
    } else if (!this.data.isAgree) {
      wx.showModal({
        title: '温馨提示',
        content: '请同意奥德曼用户登陆协议'
      })
    }else{
      var datas = {
        username: name,
        password: password 
      }
      API.login(datas).then(res=>{
        console.log(res)
      })
      wx.request({
        url: 'https://miniapp.odeman.com/summercamp/login.php',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        }, 
        method: 'POST',
        data: {
          username: name,
          password: password 
        },
        success:function(res){
          console.log(res)
          if (res.data.status =="error") {
            wx.showModal({
              title: '温馨提示',
              content: res.data.msg
            })
          } else {
            console.log(res.data.userid)
      
            // wx.setStorageSync('userid', res.data.userid)
            // wx.navigateBack({
            //   delta: 1
            // })
          }
         
           
          
        },
        fail:function(res){
          console.log(res)

        }
      })
    }
  }
})